const fp = require('lodash/fp')
const { Maybe, Container } = require('./support')

//练习1: 使用fp.add(x,y)和fp.map(f,x)创建一个能让functor里的值增加的函数ex1
let maybe = Maybe.of([5, 6, 1])
let ex1 = (num) =>{
    let fn = fp.flowRight(fp.map(fp.add(num)));
    return maybe.map(fn);  
};

//练习2: 实现一个函数 ex2 , 能够使用 fp.first 获取列表的第一个元素
let xs = Container.of(['do','ray','me','fa','so','la','ti','do']);
let ex2 = () => {
    return xs.map(fp.first)._value;
};

//练习3: 实现一个函数ex3，使用safeProp和fp.first找到user的名字的首字母
let safeProp = fp.curry(function (x, o) {
    return Maybe.of(o[x]);
});
let user = { id: 2, name: 'Albert' };
let ex3 = () => {
    return safeProp('name',user).map(fp.first)._value;
};

//练习4: 使用Maybe重写ex4,不要有if语句
let  ex4 = function(n){
    let m1 = new MayBe(n);
    let m2 = m1.map(parseInt);
    return m2._value;
};